package fei.yuzhi.com.loveflowers.fragment.shopping;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.j256.ormlite.dao.Dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import fei.yuzhi.com.loveflowers.R;


/**
 * Created by Administrator on 2016/8/9.
 */
public class ShopingFragment extends Fragment {

    @BindView(R.id.cb_shopping_allSelect)
    CheckBox cbShoppingAllSelect;
    @BindView(R.id.tv_shopping_delete)
    TextView tvShoppingDelete;
    @BindView(R.id.tv_shopping_pay)
    TextView tvShoppingPay;
    @BindView(R.id.lv_shpping_body)
    ListView lvShppingBody;
    @BindView(R.id.tv_shopping_allPress)
    TextView tvShoppingAllPress;
    @BindView(R.id.tv_shopping_title)
    TextView tvShoppingTitle;
    private ShoppingHelper helper;
    private Dao<ShoppingBean, Integer> shoppingDao;
    private double press;
    private List<ShoppingBean> shoppingBeens = new ArrayList<>();
    private ShoppingAdapter adapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_shoping, container, false);
        initHelper();
        ButterKnife.bind(this, view);

//        Button btn_fenxiang = (Button) view.findViewById(R.id.btn_fenxiang);
//        btn_fenxiang.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                //1.创建一键分享的对象
//                OnekeyShare onekeyShare=new OnekeyShare();
//                onekeyShare.disableSSOWhenAuthorize();//关闭sso授权
//                //设置一键分享的内容
//                onekeyShare.setText("一键分享的内容");
//                onekeyShare.show(getContext());
//            }
//        });

        return view;
    }

    @Override
    public void onResume() {
        super.onResume();

        try {
            shoppingBeens = shoppingDao.queryForAll();
            Log.i("tag", "1111111111111111->" + shoppingBeens.size());
        } catch (SQLException e) {
            e.printStackTrace();
        }

        adapter = new ShoppingAdapter(getContext(), shoppingBeens);
        lvShppingBody.setAdapter(adapter);
        //总价计算回调
        adapter.setOnGoodsCheckedChangeListener(new ShoppingAdapter.OnGoodsCheckedChangeListener() {
            @Override
            public void onGoodsCheckedChange(int totalCount, double totalPrice, List<ShoppingBean> newShoppingBeanList) {
                press = totalPrice;
                tvShoppingAllPress.setText(String.format(getString(R.string.total), totalPrice));
                tvShoppingPay.setText(String.format(getString(R.string.jiesuan), totalCount));
                try {
                    List<ShoppingBean> delateShoppingBeen = shoppingDao.queryForAll();
                    shoppingDao.delete(delateShoppingBeen);
                    for (int n=0;n<newShoppingBeanList.size();n++){
                        shoppingDao.create(newShoppingBeanList.get(n));
                    }

                } catch (SQLException e) {
                    e.printStackTrace();
                }

            }
        });
        //全选按钮
        cbShoppingAllSelect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (v instanceof CheckBox) {
                    CheckBox checkBox = (CheckBox) v;
                    adapter.setupAllChecked(checkBox.isChecked());
                }
            }
        });

        //全选回调
        adapter.setOnAllCheckedBoxNeedChangeListener(new ShoppingAdapter.OnAllCheckedBoxNeedChangeListener() {
            @Override
            public void onCheckedBoxNeedChange(boolean allIsChecked) {
                cbShoppingAllSelect.setChecked(allIsChecked);
            }
        });
//删除选中
        tvShoppingDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                adapter.removeSelect();
            }
        });

    }

    @Override
    public void onPause() {
        super.onPause();



    }


    @OnClick({R.id.cb_shopping_allSelect, R.id.tv_shopping_pay})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.cb_shopping_allSelect:


                break;
            case R.id.tv_shopping_pay:
                Toast.makeText(getContext(), "应付" + press, Toast.LENGTH_SHORT).show();
                break;
        }
    }


    private void initHelper() {

        try {
            helper = ShoppingHelper.getInstamce(getContext());
            shoppingDao = helper.getDao(ShoppingBean.class);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
